package com.example.findmystuff;

import com.database.findmystuff.User;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PromoteUserActivity extends Activity {
	
	Button promote, cancel;
	EditText email;
	Toast toast;
	public User user;
	/**
	 * This method is called when the activity is created
	 * @param savedInstanceState a bundle used to save the state that this instance of the activity is in.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_promote_user);
		
		promote = (Button) findViewById(R.id.promoteButton);
        cancel = (Button) findViewById(R.id.cancelButton);
        email = (EditText) findViewById(R.id.editText1);
        
        
        promote.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(MainActivity.database.getUser(email.getText().toString()) != null){
							MainActivity.database.setIsAdmin(email.getText().toString(), true);
							toast = Toast.makeText(getApplicationContext(), "Account Promoted!", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
							toast.show();
							MainActivity.database.saveObject(MainActivity.database);
							//MainActivity.database.loadSerializedObject(MainActivity.database);
							Intent i = new Intent(PromoteUserActivity.this, AdminPrivilegesActivity.class);
							startActivity(i);					
						} else{
							toast = Toast.makeText(getApplicationContext(), "Account Not Found!", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
							toast.show();
						}
					}
					});
        
        cancel.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(PromoteUserActivity.this, AdminPrivilegesActivity.class);
						startActivity(i);
					}
				});
	}
	/**
	 * Initializes the contents of the Activity's standard options menu.
	 * @param menu the standard options menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_promote_user, menu);
		return true;
	}

}
